package com.xzg.erp.system.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xzg.erp.common.BusinessException;
import com.xzg.erp.common.Constants;
import com.xzg.erp.system.entity.SysUser;
import com.xzg.erp.system.mapper.SysUserMapper;
import com.xzg.erp.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author qwc
 * @since 2025-02-04
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public boolean saveUser(SysUser sysUser) {
        SysUser user = this.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, sysUser.getUsername()));
        if (Objects.nonNull(user)) {
            throw new BusinessException(HttpStatus.CONFLICT, Constants.USER_EXIT);
        }
        sysUser.setPassword(SaSecureUtil.md5(sysUser.getPassword()));
        LocalDateTime now = LocalDateTime.now();
        sysUser.setCreatedBy(0);
        sysUser.setUpdatedBy(0);
        sysUser.setCreatedAt(now);
        sysUser.setUpdatedAt(now);
        return this.save(sysUser);
    }
}
